package com.example.test_app.view;

import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

public class LivePagerLayoutManager extends LinearLayoutManager {
    private  final String TAG = getClass().getSimpleName();
    private PagerSnapHelper mPagerSnapHelper;

    public LivePagerLayoutManager(Context context) {
        super(context);
        init();
    }

    private void init(){
        mPagerSnapHelper=new PagerSnapHelper();
    }


    @Override
    public void onAttachedToWindow(RecyclerView view) {
        super.onAttachedToWindow(view);
        mPagerSnapHelper.attachToRecyclerView(view);
        view.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(@NonNull View view) {
                Log.d(TAG, "onChildViewAttachedToWindow: "+getPosition(view));
            }

            @Override
            public void onChildViewDetachedFromWindow(@NonNull View view) {
                Log.d(TAG, "onChildViewDetachedFromWindow: "+getPosition(view));

            }
        });
    }

    /**
     * 滑动状态的改变
     * 缓慢拖拽-> SCROLL_STATE_DRAGGING
     * 快速滚动-> SCROLL_STATE_SETTLING
     * 空闲状态-> SCROLL_STATE_IDLE
     *
     * @param state
     */
    @Override
    public void onScrollStateChanged(int state) {
        switch (state) {
            case RecyclerView.SCROLL_STATE_IDLE:
                Log.d(getClass().getSimpleName(), "onScrollStateChanged: SCROLL_STATE_IDLE");
                break;
            case RecyclerView.SCROLL_STATE_SETTLING:
                Log.d(getClass().getSimpleName(), "onScrollStateChanged:SCROLL_STATE_SETTLING ");
                break;
            case RecyclerView.SCROLL_STATE_DRAGGING:
                Log.d(getClass().getSimpleName(), "onScrollStateChanged: SCROLL_STATE_DRAGGING");
                break;
            default:
                break;
        }
    }

}
